﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.statistics
{
    public partial class customerapplist : System.Web.UI.Page
    {
        protected string[] customer_Data = new string[] { "", "" };
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[DTKeys.SESSION_BUSNIESE_NUM] != null)
            {
                string businessNum = Session[DTKeys.SESSION_BUSNIESE_NUM].ToString(); 
                customer_Data = GetCustomerTimesDataByBusinessNum(30,businessNum);
            }
        }

        private string[] GetCustomerTimesDataByBusinessNum(int nday,string businessNum)
        {
            //获取最近n天的统计数据
            DateTime startday = DateTime.Now.AddDays(-nday);
            DateTime endDay = DateTime.Now.AddDays(-1);
            string strStartTime = startday.ToString("yyyy-MM-dd") + " 00:00:00.000";
            string strEndTime = endDay.ToString("yyyy-MM-dd") + " 23:59:59.999";


            BLL.statistics bll = new BLL.statistics();
            System.Data.DataSet dsCustomers = bll.GetCustomerTimesDataByBusinessNum(businessNum, strStartTime, strEndTime);

            string[] arr_series = new string[2];
            string xAxis = string.Empty;
            for (int i = 0; i < nday; i++)
            {
                xAxis += "'" + startday.AddDays(i).ToString("MM-dd") + "',";
            }
            if (xAxis.Length > 0)
            {
                xAxis = xAxis.Remove(xAxis.Length - 1, 1);
            }

            string yAxis = string.Empty;
            if (nday > 0)
            {
                BLL.sf_appidmanage bll_app = new BLL.sf_appidmanage();
                System.Data.DataSet dsAppList = bll_app.GetList(" businessNum = '" + businessNum + "'");

                for (int i = 0; i < dsAppList.Tables[0].Rows.Count; i++)
                {
                    System.Data.DataRow r = dsAppList.Tables[0].Rows[i];
                    string appNum = r["appid"].ToString();

                    string temp_yAxis = string.Empty;
                    for (int j = 0; j < nday; j++)
                    {
                        string strday = startday.AddDays(j).ToString("yyyy-MM-dd");
                        System.Data.DataRow[] rows = dsCustomers.Tables[0].Select(" appNum = '" + appNum + "' and day = '" + strday + "'");

                        if (rows.Length > 0)
                        {
                            temp_yAxis += rows[0]["count"].ToString() + ",";
                        }
                        else
                        {
                            temp_yAxis += "0,";
                        }
                    }

                    temp_yAxis = temp_yAxis.Length > 0 ? temp_yAxis.Remove(temp_yAxis.Length - 1, 1) : "";
                    yAxis += "{ type: 'spline',name:'" + r["appid_name"].ToString() + "', data: [" + temp_yAxis + "] },";
                }
            }

            return new[] { xAxis, yAxis };
        }
    }
}